/**
 * ByteQueue.java
 *
 * a simple fixed-length Queue.
 */

package org.nanhill.commons.collection;

public class ByteQueue extends Object {
	final static int Q_SIZE = 10;

	protected byte [] data;
	protected int top = 0;
	protected int bottom = 0;

	public ByteQueue() {
		data = new byte[Q_SIZE+1];
		} /* end constructor */

	public synchronized void push( byte o ) {
		data[bottom] = o;
		bottom++;
		if ( bottom >= Q_SIZE ) {
			bottom = 0;
			} /* end overflow */
		/* if wrapped around, advance top so it
		   points to the old value */
		if ( top == bottom ) { top++; }
		notifyAll();
		} /* end push() */

	public synchronized byte pop() {
                if ( top == bottom ) { /* Q is empty */
                        while ( true ) {
                         try { wait(); break; }
                         catch( InterruptedException ie ) { ; }
                         } /* end waiting loop */
                        } /* wait for push to fill Q */
		byte r = data[top];

		top++;
		if ( top >= Q_SIZE ) {
			top = 0;
			} /* end overflow */

		return r;
		} /* end pop() */

	} /* end class Queue */
